package com.easthome.controller;

import com.easthome.utils.RedisConnectionPool;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

//你好的
public class SendCodeServlet extends HttpServlet {
    //发送验证码

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        super.doPost(req, resp);
        String codes =req.getParameter("code");
        Jedis jedis = RedisConnectionPool.getJedis();
        String code = jedis.get("tel");
        if(code.equals(codes)){
            resp.getWriter().write("验证码正确");
        }
    }
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //获取手机号
        String tel = req.getParameter("tel");
        Random random= new Random();
        String code = "";
        for (int i = 0; i < 6; i++) {
            code += random.nextInt(9);
        }
        Jedis jedis = RedisConnectionPool.getJedis();

        jedis.setex(tel+"::code",60,code);

        resp.getWriter().write("success:  "+code);

        jedis.close();
    }
}
